package cn.wolfcode.service.impl;

import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.Product;
import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.feign.ProductFeignApi;
import cn.wolfcode.mapper.SeckillProductMapper;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.ISeckillProductService;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class SeckillProductServiceImpl implements ISeckillProductService {
    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private ProductFeignApi productFeignApi;

    @Override
    public List<SeckillProductVo> selectByTime(int time) {
        //通过时间查询秒杀商品数据
        List<SeckillProduct> seckillProducts = seckillProductMapper.queryCurrentlySeckillProduct(time);
        ArrayList<Long> ids = new ArrayList<>();
        for (SeckillProduct seckillProduct : seckillProducts) {
            ids.add(seckillProduct.getProductId());
        }
        //远程调用查询商品集合数据
        Result<List<Product>> results = productFeignApi.queryByIds(ids);
        List<SeckillProductVo> vos = new ArrayList<>();
        if (results != null && !results.hasError()){
            List<Product> products = results.getData();
            Map<Long,Product> maps = new HashMap<>();
            for (Product product : products) {
                maps.put(product.getId(),product);
            }
            for (SeckillProduct seckillProduct : seckillProducts) {
                SeckillProductVo vo = new SeckillProductVo();
                Product product = maps.get(seckillProduct.getProductId());
                BeanUtils.copyProperties(product,vo);
                BeanUtils.copyProperties(seckillProduct,vo);
                vos.add(vo);
            }
        }
        return vos;
    }

    @Override
    public List<SeckillProductVo> queryByTime(int time) {
        List<Object> objects = redisTemplate.opsForHash().values(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(String.valueOf(time)));
        List<SeckillProductVo> vos = new ArrayList<>();
        for (Object objson : objects) {
            vos.add(JSON.parseObject(String.valueOf(objson),SeckillProductVo.class));
        }
        return vos;
    }

    @Override
    public SeckillProductVo find(Long seckillId, int time) {
        //从redis中查询该秒杀商品
        String key = SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(String.valueOf(time));
        String obj = (String) redisTemplate.opsForHash().get(key,String.valueOf(seckillId));
        return JSON.parseObject(obj,SeckillProductVo.class);
    }

    @Override
    public void syncRedisStockCount(Integer time, Long seckillId) {
        //同步数据
        int count = seckillProductMapper.getStockCount(seckillId);
        //更新到redis缓存中
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_STOCK_COUNT_HASH.getRealKey(time+""),seckillId+"",count+"");
        System.out.println("同步数据成功");
    }
}
